import o from "./frameworkManager"
import i from "./EngineUtil"
import r from "./MatchGameData"
import n from "./MatchGameConfig"
import l, { LayerType } from "./ObstacleConfig"
import s, { Direction } from "./MatchGameDataMgr"
import c from "./SquareStatusMgr"
import u from "./MatchGameCenterCtrl"

const { ccclass, property } = cc._decorator;
@ccclass
class MatchGameUtilsDataMgr {

    static _instance;
    public static _getInstance(): MatchGameUtilsDataMgr {
        if (!MatchGameUtilsDataMgr._instance) {
            MatchGameUtilsDataMgr._instance = new MatchGameUtilsDataMgr();
        }
        return MatchGameUtilsDataMgr._instance;
    }
    public getPos = function (e, t) {
        return cc.v2(e * n.ITEM_WIDTH, -t * n.ITEM_HEIGHT);
    };
    public getV3Pos = function (e, t) {
        return cc.v3(e * n.ITEM_WIDTH, -t * n.ITEM_HEIGHT);
    };
    public getRowAndColByPos =  (e) => {
        return {
            row: Number(Math.floor(e.x / n.ITEM_WIDTH)),
            col: Number(-Math.floor(e.y / n.ITEM_HEIGHT))
        };
    };
    public loadImage = function (e, t, a, i) {
        t && e && cc.resources.load(e, function (r, n) {
            if (r) {
                o.log("加载resource错误url:" + e, r);
                i && i();
            } else {
                t && t.node && n && (t.spriteFrame = new cc.SpriteFrame(n));
                a && a();
            }
        });
    };
    public getIsSameMatchItem = function (e, t, a) {
        var o = this.getIsMatchItem(e, t);
        return o && o.itemType == a ? o : null;
    };
    public getCreateMatchType = function () {
        return i.getRandomNumber(1, 6);
    };
    public getIsPropItem = function (e, t) {
        return this.getMiddleSquareItem(e, t, n.SquareType.PropItem);
    };
    public getIsMatchItem = function (e, t) {
        return this.getMiddleSquareItem(e, t, n.SquareType.MatchItem);
    };
    public getIsNoHeightSquareMatchItem = function (e, t) {
        var a = this.getIsMatchItem(e, t);
        return a && !this.isHaveObstacleInHigherLayout(a) ? a : null;
    };
    public isHaveObstacleInHigherLayout =  (e) => {
        if (e) {
            var t, a = e._row, i = e._col;
            if (e.squareType == n.SquareType.PropItem || e.squareType == n.SquareType.MatchItem) t =LayerType.Middle; else if (e.squareType == n.SquareType.ObstacleItem) {
                var r = e.itemInfo;
                r && (t = r.LayoutType);
            }
            return !(!t || t == LayerType.Top || (t ==LayerType.Middle ? !u._instance.topSquareGrid[a] || !u._instance.topSquareGrid[a][i] || u._instance.topSquareGrid[a][i].squareType != n.SquareType.ObstacleItem || c.getSquareDestoryStatus(u._instance.topSquareGrid[a][i]) : t !=LayerType.Lowest || !u._instance.topSquareGrid[a] || !u._instance.topSquareGrid[a][i] || u._instance.topSquareGrid[a][i].squareType != n.SquareType.ObstacleItem || c.getSquareDestoryStatus(u._instance.topSquareGrid[a][i])));
        }
        o.log("障碍物上层是否有其他障碍物报错1");
    };
    public getIsSamePropItem = function (e, t, a) {
        var o = this.getIsPropItem(e, t);
        return o && o.itemType == a ? o : null;
    };
    public getMiddleSquareItem = function (e, t, a) {
        return s.middleSquareGrid[e] && s.middleSquareGrid[e][t] && s.middleSquareGrid[e][t].squareType == a ? s.middleSquareGrid[e][t] : null;
    };
    public getSquaresDirection = function (e, t) {
        return e && t ? e.row == t.row && e.col - t.col == 1 ? Direction.Up : e.row == t.row && e.col - t.col == -1 ? Direction.Down : e.col == t.col && e.row - t.row == 1 ? Direction.Left : e.col == t.col && e.row - t.row == -1 ? Direction.Right : Direction.Null : Direction.Null;
    };
    public getNeighborSquareByType = function (e, t, a) {
        var o = [];
        this.getMiddleSquareItem(e, t + 1, a) && o.push(this.getMiddleSquareItem(e, t + 1, a));
        this.getMiddleSquareItem(e, t - 1, a) && o.push(this.getMiddleSquareItem(e, t - 1, a));
        this.getMiddleSquareItem(e + 1, t, a) && o.push(this.getMiddleSquareItem(e + 1, t, a));
        this.getMiddleSquareItem(e - 1, t, a) && o.push(this.getMiddleSquareItem(e - 1, t, a));
        return o;
    };
    public getHalfNeighborSquareByType = function (e, t, a) {
        var o = [];
        this.getMiddleSquareItem(e, t + 1, a) && o.push(this.getMiddleSquareItem(e, t + 1, a));
        this.getMiddleSquareItem(e + 1, t, a) && o.push(this.getMiddleSquareItem(e + 1, t, a));
        return o;
    };
    public isMatchitem =  (e) => {
        return !(!e || e.squareType != n.SquareType.MatchItem);
    };
    public isMatchsSame =  (e) => {
        var t = this;
        if (this.isMatchitem(e[0])) {
            var a = e[0].itemType, o = true;
            e.forEach( (e) => {
                t.isMatchitem(e) && e.itemType === a || (o = false);
            });
            return o;
        }
        return false;
    };
    public getMiddleGridItem = function (e, t) {
        return u._instance.getMiddleGridItem(e, t);
    };
    public getMatchItem = function (e, t) {
        return u._instance.getMatchItem(e, t);
    };
    public getPropItem = function (e, t) {
        return u._instance.getPropItem(e, t);
    };
    public getNormalPropItem = function (e, t) {
        return u._instance.getNormalPropItem(e, t);
    };
    public logMap = function () {
        if (r.isEnableDebug()) {
            for (var e = "", t = 0; t < s._map_col; t++) {
                t > 0 && (e += "\n");
                for (var a = 0; a < s._map_row; a++) if (s.middleSquareGrid[a] && s.middleSquareGrid[a][t]) {
                    e += " " + s.middleSquareGrid[a][t].squareType + s.middleSquareGrid[a][t].itemType;
                    !c.getSquareNormalStatus(s.middleSquareGrid[a][t]) || s.middleSquareGrid[a][t].canMove;
                } else e += "   ";
            }
            o.log("地图信息：\n" + e);
        }
    };
    public logStateMap = function () {
        if (r.isEnableDebug()) {
            for (var e = "", t = 0; t < s._map_col; t++) {
                t > 0 && (e += "\n");
                for (var a = 0; a < s._map_row; a++) if (s.middleSquareGrid[a] && s.middleSquareGrid[a][t]) {
                    e += " " + s.middleSquareGrid[a][t].squareFsm.state;
                    !c.getSquareNormalStatus(s.middleSquareGrid[a][t]) || s.middleSquareGrid[a][t].canMove;
                } else e += "   ";
            }
            o.log("状态地图信息：\n" + e);
        }
    };
    public logViewMap = function () {
        for (var e = [], t = u._instance.ui.MiddleParents, a = 0; a < t.children.length; a++) {
            var i = t.children[a];
            if (i) {
                var r = i.getComponent("MatchItemPrefabCtrl") || i.getComponent("PropPrefabCtrl") || i.getComponent("ObstaclePrefabCtrl") || i.getComponent("MultiObstaclePrefabCtrl") || i.getComponent("EmptyObstacleItemCtrl");
                e.push(r);
            }
        }
        for (var n = "", l = 0; l < s._map_col; l++) {
            l > 0 && (n += "\n");
            for (var c = 0; c < s._map_row; c++) {
                var p = [];
                for (a = 0; a < e.length; a++) if ((r = e[a]) && l == r._col && c == r._row) {
                    var d = this.getRowAndColByPos(cc.v3(r.node.position));
                    d.col != r._col && d.row != r._row && o.error("棋子的位置不对");
                    p.push(r);
                }
                if (p.length > 0) if (1 == p.length) {
                    var h = p[0];
                    n += " " + h.squareType + h.itemType;
                } else {
                    o.error("该位置方块重复==", "row==" + c, "col==" + l);
                    for (a = 0; a < p.length; a++) {
                        r = p[a];
                        var f = s.middleSquareGrid[c] && s.middleSquareGrid[c][l];
                        r.name != f.name && r.node.removeFromParent(true);
                    }
                } else n += "   ";
            }
        }
        o.log("UI展示信息：\n" + n);
    };
}
export default MatchGameUtilsDataMgr._getInstance();